{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check if All Characters Have Equal Number of Occurrences"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: areOccurrencesEqual"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检查是否所有字符出现次数相同"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串 <code>s</code> ，如果 <code>s</code> 是一个 <strong>好</strong> 字符串，请你返回 <code>true</code> ，否则请返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>如果 <code>s</code> 中出现过的 <strong>所有</strong> 字符的出现次数 <strong>相同</strong> ，那么我们称字符串 <code>s</code> 是 <strong>好</strong> 字符串。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>s = \"abacbc\"\n",
    "<b>输出：</b>true\n",
    "<b>解释：</b>s 中出现过的字符为 'a'，'b' 和 'c' 。s 中所有字符均出现 2 次。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>s = \"aaabb\"\n",
    "<b>输出：</b>false\n",
    "<b>解释：</b>s 中出现过的字符为 'a' 和 'b' 。\n",
    "'a' 出现了 3 次，'b' 出现了 2 次，两者出现次数不同。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 1000</code></li>\n",
    "\t<li><code>s</code> 只包含小写英文字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-all-characters-have-equal-number-of-occurrences](https://leetcode.cn/problems/check-if-all-characters-have-equal-number-of-occurrences/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-all-characters-have-equal-number-of-occurrences](https://leetcode.cn/problems/check-if-all-characters-have-equal-number-of-occurrences/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abacbc\"', '\"aaabb\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        return len(set(Counter(s).values()))==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        strList = list(s)\n",
    "        a = Counter(strList)\n",
    "        result = []\n",
    "        for key, value in a.items():\n",
    "            result.append(value)\n",
    "        \n",
    "        if len(list(set(result)))== 1:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        L=list(set(s))\n",
    "        for i in range(len(L)-1):\n",
    "            if s.count(L[i])==s.count(L[i+1]):\n",
    "                pass\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        # cnt = Counter(s)\n",
    "        # fre = len(s) // len(cnt)\n",
    "        # return all(fre == v for v in cnt.values())\n",
    "\n",
    "        # cnt = Counter(s)\n",
    "        # st = set(cnt.values())\n",
    "        # return len(st) == 1\n",
    "\n",
    "\n",
    "        return len(set(Counter(s).values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        c = Counter(s)\n",
    "        t = 0\n",
    "        for x in c.values():\n",
    "            if t and t != x:\n",
    "                return False\n",
    "            t = x\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "\n",
    "        s =dict( Counter(s))\n",
    "        res = []\n",
    "        value1=s.values()\n",
    "        res=list(value1)\n",
    "        \n",
    "        if len(set(res)) != 1:\n",
    "            return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        res = collections.Counter(s).values()\n",
    "        if len(set(list(res)))==1:\n",
    "            return True\n",
    "        else:\n",
    "            return False    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        cnt = Counter(s)\n",
    "        fre = len(s) // len(cnt)\n",
    "        return all(fre == v for v in cnt.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        a={}\n",
    "        for i in s:\n",
    "            if i in a:\n",
    "                a[i]+=1\n",
    "            else:\n",
    "                a[i]=1\n",
    "        if len(set(a.values()))==1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        cnt=Counter(s)\n",
    "        return len(set(cnt.values()))==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        dr = {}\n",
    "        for i in s:\n",
    "            if i in dr:\n",
    "                dr[i] += 1\n",
    "            else:\n",
    "                dr[i] = 1\n",
    "        se = set(dr.values())\n",
    "        if len(se) > 1:\n",
    "            return False\n",
    "        else:\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        # cnt = Counter(s)\n",
    "        # fre = len(s) // len(cnt)\n",
    "        # return all(fre == v for v in cnt.values())\n",
    "\n",
    "        cnt = Counter(s)\n",
    "        st = set(cnt.values())\n",
    "        return len(st) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        # cnt = Counter(s)\n",
    "        # fre = len(s) // len(cnt)\n",
    "        # return all(fre == v for v in cnt.values())\n",
    "\n",
    "        cnt = Counter(s)\n",
    "        st = set(cnt.values())\n",
    "        return len(st) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        counts = defaultdict(int)\n",
    "        for char in s:\n",
    "            counts[char] += 1\n",
    "        return len(set(counts.values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        lst = [0] * 26\n",
    "        for c in s:\n",
    "            lst[ord(c) - 97] += 1\n",
    "        a = 0\n",
    "        for n in lst:\n",
    "            if n != 0 and a == 0:\n",
    "                a = n\n",
    "            elif n != 0 and n != a:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        # 创建字典。用来保存出现频次\n",
    "        dict1 = {}\n",
    "\n",
    "        # 遍历字符串\n",
    "        for i in s:\n",
    "            # 判断字符是否存在于字典中\n",
    "            if i not in dict1:\n",
    "                dict1[i]=1  # 若不存在，则存进去，value设为1\n",
    "            else:\n",
    "                dict1[i]+=1 # 若存在，则把 value+1\n",
    "        # 最后，通过判断返回\n",
    "        \"\"\"\n",
    "        1.方法 dict.value() 以列表形式 返回字典中的所有value\n",
    "        2.函数 set() 删除重复元素，返回集合\n",
    "        3.函数 len() 计算长度\n",
    "        逻辑是 如果每个字符出现频次相同，那么len(set(dict1.values()))是等于1的，反之，不等于1，因此作为判断语句\n",
    "\n",
    "        \"\"\"    \n",
    "        return len(set(dict1.values()))==1\n",
    "    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        return bool(eval('=='.join(map(str,Counter(s).values()))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        num_set = set()\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            num_set.add(s.count(s[i]))\n",
    "\n",
    "            if len(num_set) > 1:\n",
    "                return False\n",
    "        \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        counter=Counter(s)\n",
    "        e='=='.join(map(str,counter.values()))\n",
    "        return bool(eval(e))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        cnt = Counter(s)\n",
    "        return len(set(cnt.values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        return len(set(Counter(s).values())) == 1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\r\n",
    "        from collections import Counter\r\n",
    "        s = Counter(s)\r\n",
    "        return len(set(s.values())) == 1\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        return len(set(Counter(s).values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        ds = Counter(s)\n",
    "        c = 0\n",
    "        for v in ds.values():\n",
    "            if c == 0:\n",
    "                c = v\n",
    "            else:\n",
    "                if v != c:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        c = Counter(s)\n",
    "        num = c[s[0]]\n",
    "        for ss in set(s):\n",
    "            if c[ss] != num:\n",
    "                return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        '''\n",
    "        # 哈希表\n",
    "        cnt = dict()\n",
    "        for ch in s:\n",
    "            cnt[ch] = cnt.get(ch, 0) + 1\n",
    "        return len(set(cnt.values())) == 1\n",
    "        '''\n",
    "\n",
    "        # 直接计数\n",
    "        cnt = Counter(s)\n",
    "        return len(set(cnt.values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        a=s[0]\n",
    "        s=Counter(s)\n",
    "        print(s)\n",
    "        print(s[a])\n",
    "        for value in s.values():\n",
    "            if value!=s[a]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        return len(set(Counter(s).values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        a={}\n",
    "        for i in s:\n",
    "            if i in a:\n",
    "                a[i]+=1\n",
    "            else:\n",
    "                a[i]=1\n",
    "        if len(set(a.values()))==1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        return len(set(Counter(s).values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        freq = Counter(s)\n",
    "        occ = len(s)//len(freq)\n",
    "        return all(v==occ for v in freq.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        sc=Counter(s)\n",
    "        count=set()\n",
    "        for k,v in sc.items():\n",
    "            count.add(v)\n",
    "        if len(count)==1:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        list_s = list(s)\n",
    "        c = list_s.count(list_s[0])\n",
    "        for i in list_s:\n",
    "            if list_s.count(i) != c:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        count=Counter()\n",
    "        for i in s:\n",
    "            count[i]+=1\n",
    "        n=-1\n",
    "        for v in count.values():\n",
    "            if n>=0 and n != v: return False\n",
    "            n = v\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        freq = Counter(s)   # 每个字符的实际频数\n",
    "        occ = len(s) // len(freq)    # 每个字符的理论频数\n",
    "        return all(v == occ for v in freq.values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        cnt_dic = collections.defaultdict(int)\n",
    "        for i in s:\n",
    "            cnt_dic[i] += 1\n",
    "        return len(set(cnt_dic.values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "      temp = []\n",
    "      for i in s:\n",
    "        temp.append(i)\n",
    "      temp = list(set(temp))\n",
    "      count = s.count(temp[0])\n",
    "      for t in temp:\n",
    "        if s.count(t) != count:\n",
    "          return False\n",
    "      return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        c = Counter(s)\n",
    "        x = c[s[0]]\n",
    "        return all(v==x for v in c.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        dic = Counter(s)\n",
    "        return len(set(dic.values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        return len(set(Counter(s).values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        freq = Counter(s)\n",
    "        occ = len(s) // len(freq)\n",
    "        return all(v == occ for v in freq.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        l = list(s)\n",
    "        for x in s:\n",
    "           if  s.count(x)==l.count(l[0]):\n",
    "               continue\n",
    "           else:\n",
    "               return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        a=s[0]\n",
    "        s=Counter(s)\n",
    "        for value in s.values():\n",
    "            if value!=s[a]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        cnt=Counter(s)\n",
    "        n=len(s)/len(cnt)\n",
    "        for i in cnt:\n",
    "            if cnt[i]!=n:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        tmp=Counter(s)\n",
    "        return len(set(list(tmp.values())))==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        c = collections.Counter(s)\n",
    "        ans = list(c.items())\n",
    "        a = ans[0][1]\n",
    "        for p,s in ans:\n",
    "            if s != a:\n",
    "                return False\n",
    "            else:\n",
    "                a = s\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        counts={}\n",
    "        for c in s:\n",
    "            counts[c]=counts.get(c,0)+1\n",
    "        if len(s)%len(counts)!=0:\n",
    "            return False\n",
    "        count=len(s)/len(counts)\n",
    "        for c,cc in counts.items():\n",
    "            if cc!=count:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        dic = {}\n",
    "        for i in s:\n",
    "            dic[i] = s.count(i)\n",
    "        target = dic[s[0]]\n",
    "        for i in dic:\n",
    "            if target != dic[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        return len(set(Counter(s).values())) ==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        a={}\n",
    "        for i in s:\n",
    "            if i in a:\n",
    "                a[i]+=1\n",
    "            else:\n",
    "                a[i]=1\n",
    "        if len(set(a.values()))==1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        dic = {i:0 for i in s}\n",
    "        for i in s:\n",
    "            dic[i] += 1\n",
    "        return len(set(dic.values())) == 1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        freq = Counter(s)\n",
    "        occ = len(s)//len(freq)\n",
    "        return all(v == occ for v in freq.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\r\n",
    "        return len(set(Counter(s).values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        res = True\n",
    "        times = int(len(s)/len(set(s)))\n",
    "        for i in set(s):\n",
    "            counter = 0\n",
    "            for j in s:\n",
    "                if i == j:\n",
    "                    counter += 1\n",
    "            if counter != times:\n",
    "                res = False\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        a={}\n",
    "        for i in s:\n",
    "            if i in a:\n",
    "                a[i]+=1\n",
    "            else:\n",
    "                a[i]=1\n",
    "        if len(set(a.values()))==1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        count = Counter(s)\n",
    "\n",
    "        for k in count:\n",
    "            if count[k] != count[s[0]]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        cnts = {}\n",
    "        for c in s:\n",
    "            cnts[c] = cnts.get(c, 0) + 1\n",
    "        \n",
    "        cnt_set = set(cnts.values())\n",
    "        return len(cnt_set) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        # 哈希表\n",
    "        cnt = dict()\n",
    "        for ch in s:\n",
    "            cnt[ch] = cnt.get(ch, 0) + 1\n",
    "        return len(set(cnt.values())) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        cnt = Counter(s)\n",
    "        lst = None\n",
    "        for k, v in cnt.items():\n",
    "            if lst is None:\n",
    "                lst = v\n",
    "            if lst != v:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        c = collections.Counter(s)\n",
    "        cnt = set()\n",
    "        for _, v in c.items():\n",
    "            cnt.add(v)\n",
    "        return len(cnt) == 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        aa=[i for i in Counter(s).values()]\n",
    "        return max(aa)==min(aa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        freq = Counter(s)   # 每个字符的实际频数\n",
    "        occ = len(s) // len(freq)    # 每个字符的理论频数\n",
    "        return all(v == occ for v in freq.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        c=set(s)\n",
    "        l=list(s)\n",
    "        a=list(c)\n",
    "        b=[]\n",
    "        for i in a:\n",
    "           b.append(l.count(i))\n",
    "        b=set(b)\n",
    "        if len(b)==1:\n",
    "            return True\n",
    "        else:\n",
    "            return False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        m = Counter(s)\n",
    "        return len(set(list(m.values()))) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        chrDict = collections.defaultdict(int)\n",
    "        for ch in s:\n",
    "            chrDict[ch] += 1\n",
    "        \n",
    "        for i in chrDict.values():\n",
    "            if i * len(chrDict) != len(s):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        freq = Counter(s)   # 每个字符的实际频数\n",
    "        occ = len(s) // len(freq)    # 每个字符的理论频数\n",
    "        return all(v == occ for v in freq.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areOccurrencesEqual(self, s: str) -> bool:\n",
    "        prev = None\n",
    "        counts = collections.Counter(s)\n",
    "        for k, v in counts.items():\n",
    "            if prev and prev != v:\n",
    "                return False\n",
    "            if not prev:\n",
    "                prev = v\n",
    "        return True"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
